<?php

namespace App\Service\UserCenter;

use App\Constants\ErrorCode;
use App\Event\UserOnlineEvent;
use App\Model\CloseFriend;
use App\Model\Guild\GuildHost;
use App\Model\ProfileVerifyLog;
use App\Model\SocialAccount;
use App\Model\User;
use App\Model\UserActLog;
use App\Model\UserFollow;
use App\Model\UserProfile;
use App\Model\UserProfilePersonal;
use App\Model\UserSetting;
use App\Model\UserVisit;
use App\Service\BaseService;
use Psr\EventDispatcher\EventDispatcherInterface;

/**
 * 我的个人资料
 */
class MyProfileService extends BaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        $appId = $this->request->input('appid', 1);
        //基本资料
        $profile = UserProfile::getProfile($userId, true, false);
        if (!$profile) {
            log_user_result($userId, "NoUserFind");
            return $this->setError(ErrorCode::LOGIN_TOKEN_EXP, '登录过期，从重新登录！');
        }

        $user = $this->getUser();
        $ret = UserProfile::buildProfileDetail($profile, false);
        //头像审核状态
        $ret['avatar_status'] = UserActLog::getLastAvatar($userId, 'status');
        $ret['call_permit'] = $profile->call_permit;
        $ret['video_permit'] = $profile->video_permit;
        //关注数
        $ret['follow_num'] = UserFollow::getFollowNum($userId);
        //粉丝数
        $ret['fans_num'] = UserFollow::getFansNum($userId);
        //访客数
        $ret['visit_num'] = UserVisit::getNum($userId);

        $ret['friend_num'] = count(CloseFriend::getFrdList($userId));
        //绑定信息
        $personInfo = UserProfilePersonal::getProfile($userId);
        $ret['withdraw_info'] = !$personInfo ? null : [
            'real_name' => $personInfo['real_name'],
            'id_card' => $personInfo['id_card'],
            'phone' => $personInfo['phone'],
            // 'id_card_pics' => $personInfo['id_card_pics'] ?? null,
            'alipay_account' => $personInfo['alipay_account'],
        ];
        //我的邀请链接
        $forbidInvite = intval(env('FORBID_INVITE_LINK', 0));

        //屏蔽邀请链接
        $ret['invite_link'] = $forbidInvite
            ? env('INVITE_FORBID_H5_LINK', 'http://www.hudongailian.com/sxl/end.html')
            : env('INVITE_H5_LINK', 'http://www.hudongailian.com/invite2/moliao.html');

        $ret['invite_link'] .= '?invitecode=' . $ret['user_profile_id'] . '&appid=' . $this->appId;

        //自动消息设置的浮窗
        $ret['auto_msg_setting'] = UserProfile::checkIsHost($userId) ? 1 : 0;

        //账号是否绑定
        $checkQQ = SocialAccount::checkUser('qq', $userId);
        $checkWx  = SocialAccount::checkUser('weixin', $userId);
        // $checkApple  = SocialAccount::checkUser('apple', $userId);
        $ret['bind_phone'] = $user->phone;
        $ret['bind_qq'] = $checkQQ ? 1 : 0;
        $ret['bind_weixin'] = $checkWx ? 1 : 0;
        $ret['bind_apple'] = 0;

        //自我介绍审核
        $ret['self_intro_status'] = ProfileVerifyLog::checkVerified($userId, ProfileVerifyLog::TYPE_SELF_INTRO);
        //语音
        $ret['voice'] && $ret['voice']['status'] = ProfileVerifyLog::checkVerified($userId, ProfileVerifyLog::TYPE_VOICE);
        //昵称状态
        $ret['nick_name_status'] = $ret['nick_name'] == '违规昵称' ? 1 : ProfileVerifyLog::checkVerified($userId, ProfileVerifyLog::TYPE_NICKNAME);
        // log_user_result($userId, $ret);
        $this->packShowPermit($ret, $appId);

        //真人，实名，VIP
        $ret['real_person'] = UserProfilePersonal::isFaceVerified(UserProfilePersonal::getProfile($userId));
        $ret['is_vip'] = 0;
        $ret['id_verifing'] = UserProfilePersonal::checkVerifing($userId);
        //是否超级管理员
        $ret['is_super_admin'] = User::isSuperAdmin($userId);
        log_user_result($userId, $ret);
        return $ret;
    }

    /**
     * 允许显示权限
     */
    function packShowPermit(&$ret = [], $appId = 1)
    {
        // if (!User::checkValidVersion($this->curVersion, $appId, ['1.2.7'])) return false;
        $ret =  array_merge($ret, UserSetting::getShowPermit($ret['user_id']));
    }
}
